package com.tools.cleanmaster.ui.card.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.nineoldandroids.animation.AnimatorSet;
import com.nineoldandroids.animation.ObjectAnimator;
import com.tools.cleanmaster.R;
import com.tools.cleanmaster.common.thread.ThreadPool;


public class GPlayRatingStar extends LinearLayout {

    private final static int STAR_COUNT = 5;
    private final static int DURATION_STAR_ANIMATION = 800;

    private View[] mStar = new View[STAR_COUNT];

    private ObjectAnimator[] mAlphaStar = new ObjectAnimator[STAR_COUNT];

    public GPlayRatingStar(Context context, AttributeSet attrs) {
        super(context, attrs);
        inflate(context, R.layout.gp_guide_ratestar, this);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mStar[0] = findViewById(R.id.star_1);
        mStar[1] = findViewById(R.id.star_2);
        mStar[2] = findViewById(R.id.star_3);
        mStar[3] = findViewById(R.id.star_4);
        mStar[4] = findViewById(R.id.star_5);
    }

    public void startAnimation() {
        for (int i = 0; i < STAR_COUNT; i++) {
            mAlphaStar[i] = ObjectAnimator.ofFloat(mStar[i], "alpha", 0.0f, 1.0f);
        }
        ThreadPool.runOnUi(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < STAR_COUNT; i++) {
                    mStar[i].setVisibility(View.VISIBLE);
                    AnimatorSet set = new AnimatorSet();
                    set.playTogether(mAlphaStar[i]);
                    set.setDuration(DURATION_STAR_ANIMATION);
                    set.start();
                }
            }
        });
    }
}
